home *** CD-ROM | disk | FTP | other *** search
/ SVM Mac 58 / CD-ROM N°58.iso / navigateurs / Netscape Folder / chrome / messengercompose / content / default / addressingWidgetOverlay.js < prev    next >
Encoding:
JavaScript  |  2000-04-19  |  13.2 KB  |  530 lines  |  [TEXT/MOSS]

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is mozilla.org code.
  13.  *
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 1998 Netscape Communications Corporation. All
  17.  * Rights Reserved.
  18.  *
  19.  * Contributor(s): 
  20.  */
  21.  
  22. function FillRecipientTypeCombobox()
  23. {
  24.     top.MAX_RECIPIENTS = 1;
  25.     
  26.     var body;
  27.  
  28.     for ( var row = 2; row <= top.MAX_RECIPIENTS; row++ )
  29.     {
  30.         body = document.getElementById('addressWidgetBody');
  31.         
  32.         awCopyNode(awGetTreeItem(1), body, 0);
  33.     }
  34. }
  35.  
  36. function Recipients2CompFields(msgCompFields)
  37. {
  38.     if (msgCompFields)
  39.     {
  40.         var i = 1;
  41.         var addrTo = "";
  42.         var addrCc = "";
  43.         var addrBcc = "";
  44.         var addrReply = "";
  45.         var addrNg = "";
  46.         var addrFollow = "";
  47.         var addrOther = "";
  48.         var to_Sep = "";
  49.         var cc_Sep = "";
  50.         var bcc_Sep = "";
  51.         var reply_Sep = "";
  52.         var ng_Sep = "";
  53.         var follow_Sep = "";
  54.  
  55.         while ((inputField = awGetInputElement(i)))
  56.         {
  57.             fieldValue = inputField.value;
  58.             if (fieldValue != "")
  59.             {
  60.                 switch (awGetPopupElement(i).value)
  61.                 {
  62.                     case "addr_to"            : addrTo += to_Sep + fieldValue; to_Sep = ",";                    break;
  63.                     case "addr_cc"            : addrCc += cc_Sep + fieldValue; cc_Sep = ",";                    break;
  64.                     case "addr_bcc"            : addrBcc += bcc_Sep + fieldValue; bcc_Sep = ",";                break;
  65.                     case "addr_reply"        : addrReply += reply_Sep + fieldValue; reply_Sep = ",";            break;
  66.                     case "addr_newsgroups"    : addrNg += ng_Sep + fieldValue; ng_Sep = ",";                    break;
  67.                     case "addr_followup"    : addrFollow += follow_Sep + fieldValue; follow_Sep = ",";        break;
  68.                     case "addr_other"        : addrOther += other_header + ": " + fieldValue + "\n";         break;
  69.                 }
  70.             }
  71.             i ++;
  72.         }
  73.         msgCompFields.SetTo(addrTo);
  74.         msgCompFields.SetCc(addrCc);
  75.         msgCompFields.SetBcc(addrBcc);
  76.         msgCompFields.SetReplyTo(addrReply);
  77.         msgCompFields.SetNewsgroups(addrNg);
  78.         msgCompFields.SetFollowupTo(addrFollow);
  79.         msgCompFields.SetOtherRandomHeaders(addrOther);
  80.     }
  81.     else
  82.         dump("Message Compose Error: msgCompFields is null (ExtractRecipients)");
  83. }
  84.  
  85. function CompFields2Recipients(msgCompFields)
  86. {
  87.     if (msgCompFields)
  88.     {
  89.         var row = 1;
  90.         
  91.         row = awSetInputAndPopupFromArray(row, msgCompFields.SplitRecipients(msgCompFields.GetTo(), false), "addr_to");
  92.         row = awSetInputAndPopupFromArray(row, msgCompFields.SplitRecipients(msgCompFields.GetCc(), false), "addr_cc");
  93.         row = awSetInputAndPopupFromArray(row, msgCompFields.SplitRecipients(msgCompFields.GetBcc(), false), "addr_bcc");
  94.         row = awSetInputAndPopupFromArray(row, msgCompFields.SplitRecipients(msgCompFields.GetReplyTo(), false), "addr_reply");
  95.         row = awSetInputAndPopup(row, msgCompFields.GetOtherRandomHeaders(), "addr_other");
  96.         row = awSetInputAndPopup(row, msgCompFields.GetNewsgroups(), "addr_newsgroups");
  97.         row = awSetInputAndPopup(row, msgCompFields.GetFollowupTo(), "addr_followup");
  98.         
  99.         if ( row > 1 )   row--;
  100.             
  101.         // remove extra rows
  102.         while ( top.MAX_RECIPIENTS > row )
  103.             awRemoveRow(top.MAX_RECIPIENTS);
  104.         
  105.         setTimeout("awFinishCopyNodes();", 0);
  106.     }
  107. }
  108.  
  109. function awSetInputAndPopup(firstRow, inputValue, popupValue)
  110. {
  111.     var        row = firstRow;
  112.     
  113.     if ( inputValue && popupValue )
  114.     {
  115.         var addressArray = inputValue.split(",");
  116.         
  117.         for ( var index = 0; index < addressArray.length; index++ )
  118.         {
  119.             // remove leading spaces
  120.             while ( addressArray[index][0] == " " )
  121.                 addressArray[index] = addressArray[index].substring(1, addressArray[index].length);
  122.             
  123.             // we can add one row if trying to add just beyond current size
  124.             if ( row == (top.MAX_RECIPIENTS + 1))
  125.             {
  126.                 var body = document.getElementById('addressWidgetBody');
  127.                 awCopyNode(awGetTreeItem(1), body, 0);
  128.                 top.MAX_RECIPIENTS++;
  129.             }
  130.             
  131.             // if row is legal then set the values
  132.             if ( row <= top.MAX_RECIPIENTS )
  133.             {
  134.                 awGetInputElement(row).value = addressArray[index];
  135.                 awGetPopupElement(row).value = popupValue;
  136.                 row++;
  137.             }
  138.         }
  139.         
  140.         return(row);
  141.     }
  142.     return(firstRow);        
  143. }
  144.  
  145. function awSetInputAndPopupFromArray(firstRow, inputArray, popupValue)
  146. {
  147.     var        row = firstRow;
  148.     
  149.     if ( inputArray && popupValue )
  150.     {
  151.         for ( var index = 0; index < inputArray.count; index++ )
  152.         {
  153.             // remove leading spaces
  154.             inputValue = inputArray.StringAt(index);
  155.             while ( inputValue[0] == " " )
  156.                 inputValue = inputValue.substring(1, inputValue.length);
  157.             
  158.             // we can add one row if trying to add just beyond current size
  159.             if ( row == (top.MAX_RECIPIENTS + 1))
  160.             {
  161.                 var body = document.getElementById('addressWidgetBody');
  162.                 awCopyNode(awGetTreeItem(1), body, 0);
  163.                 top.MAX_RECIPIENTS++;
  164.             }
  165.             
  166.             // if row is legal then set the values
  167.             if ( row <= top.MAX_RECIPIENTS )
  168.             {
  169.                 awGetInputElement(row).value = inputValue;
  170.                 awGetPopupElement(row).value = popupValue;
  171.                 row++;
  172.             }
  173.         }
  174.         
  175.         return(row);
  176.     }
  177.     return(firstRow);        
  178. }
  179.  
  180. function awNotAnEmptyArea(event)
  181. {
  182.     //This is temporary until i figure out how to ensure to always having an empty space after the last row
  183.     dump("awNotAnEmptyArea\n");
  184.  
  185.     var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
  186.     if ( lastInput && lastInput.value )
  187.         awAppendNewRow(false);
  188.  
  189.     event.preventBubble();
  190. }
  191.  
  192. function awClickEmptySpace(setFocus)
  193. {
  194.     dump("awClickEmptySpace\n");
  195.     var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
  196.  
  197.     if ( lastInput && lastInput.value )
  198.         awAppendNewRow(setFocus);
  199.     else
  200.         if (setFocus)
  201.             awSetFocus(top.MAX_RECIPIENTS, lastInput);
  202. }
  203.  
  204. function awReturnHit(inputElement)
  205. {
  206.     var row = awGetRowByInputElement(inputElement);
  207.     
  208.     if ( inputElement.value )
  209.     {
  210.         var nextInput = awGetInputElement(row+1);
  211.         if ( !nextInput )
  212.             awAppendNewRow(true);
  213.         else
  214.             awSetFocus(row+1, nextInput);
  215.     }
  216. }
  217.  
  218. function awInputChanged(inputElement)
  219. {
  220.     dump("awInputChanged\n");
  221.     AutoCompleteAddress(inputElement);
  222.  
  223.     //Do we need to add a new row?
  224.     var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
  225.     if ( lastInput && lastInput.value && !top.doNotCreateANewRow)
  226.         awAppendNewRow(false);
  227.     top.doNotCreateANewRow = false;
  228. }
  229.  
  230. function awAppendNewRow(setFocus)
  231. {
  232.     var body = document.getElementById('addressWidgetBody');
  233.     var treeitem1 = awGetTreeItem(1);
  234.     
  235.     if ( body && treeitem1 )
  236.     {
  237.         awCopyNode(treeitem1, body, 0);
  238.         top.MAX_RECIPIENTS++;
  239.  
  240.         // focus on new input widget
  241.         if (setFocus)
  242.         {
  243.             var newInput = awGetInputElement(top.MAX_RECIPIENTS);
  244.             if ( newInput )
  245.                 awSetFocus(top.MAX_RECIPIENTS, newInput);
  246.         }
  247.     }
  248. }
  249.  
  250.  
  251. // functions for accessing the elements in the addressing widget
  252.  
  253. function awGetPopupElement(row)
  254. {
  255.     var treerow = awGetTreeRow(row);
  256.     
  257.     if ( treerow )
  258.     {
  259.         var popup = treerow.getElementsByTagName('SELECT');
  260.         if ( popup && popup.length == 1 )
  261.             return popup[0];
  262.     }
  263.     return 0;
  264. }
  265.  
  266. function awGetInputElement(row)
  267. {
  268.     var treerow = awGetTreeRow(row);
  269.     
  270.     if ( treerow )
  271.     {
  272.         var input = treerow.getElementsByTagName('INPUT');
  273.         if ( input && input.length == 1 )
  274.             return input[0];
  275.     }
  276.     return 0;
  277. }
  278.  
  279. function awGetTreeRow(row)
  280. {
  281.     var body = document.getElementById('addressWidgetBody');
  282.     
  283.     if ( body && row > 0)
  284.     {
  285.         var treerows = body.getElementsByTagName('treerow');
  286.         if ( treerows && treerows.length >= row )
  287.             return treerows[row-1];
  288.     }
  289.     return 0;
  290. }
  291.  
  292. function awGetTreeItem(row)
  293. {
  294.     var body = document.getElementById('addressWidgetBody');
  295.     
  296.     if ( body && row > 0)
  297.     {
  298.         var treeitems = body.getElementsByTagName('treeitem');
  299.         if ( treeitems && treeitems.length >= row )
  300.             return treeitems[row-1];
  301.     }
  302.     return 0;
  303. }
  304.  
  305. function awGetRowByInputElement(inputElement)
  306. {
  307.     if ( inputElement )
  308.     {
  309.         var treerow;
  310.         var inputElementTreerow = inputElement.parentNode.parentNode;
  311.         
  312.         if ( inputElementTreerow )
  313.         {
  314.             for ( var row = 1;  (treerow = awGetTreeRow(row)); row++ )
  315.             {
  316.                 if ( treerow == inputElementTreerow )
  317.                     return row;
  318.             }
  319.         }
  320.     }
  321.     return 0;
  322. }
  323.  
  324.  
  325. // Copy Node - copy this node and insert ahead of the (before) node.  Append to end if before=0
  326. function awCopyNode(node, parentNode, beforeNode)
  327. {
  328.     dump("awCopyNode\n");
  329.  
  330.         // XXXwaterson Ideally, we'd just do this, but for bug 26528
  331.         // (and some of the funky logic about the 'id' attribute in
  332.         // awCopyNodeAndChildren).
  333.         //
  334.     //var newNode = node.cloneNode(true);
  335.     var newNode = awCopyNodeAndChildren(node);
  336.     
  337.     if ( beforeNode )
  338.         parentNode.insertBefore(newNode, beforeNode);
  339.     else
  340.         parentNode.appendChild(newNode);
  341. }
  342.  
  343. function awCopyNodeAndChildren(node)
  344. {
  345.     var newNode;
  346.     if ( node.nodeName == "#text" )
  347.     {
  348.         // create new text node
  349.         newNode = document.createTextNode(node.data);
  350.     }
  351.     else
  352.     {
  353.         // create new node
  354.         if ( node.nodeName[0] >= 'A' && node.nodeName[0] <= 'Z' )
  355.             newNode = createHTML(node.nodeName);
  356.         else
  357.             newNode = document.createElement(node.nodeName);
  358.         
  359.         var attributes = node.attributes;
  360.         
  361.         if ( attributes && attributes.length )
  362.         {
  363.             var attrNode, name, value;
  364.             
  365.             // copy attributes into new node
  366.             for ( var index = 0; index < attributes.length; index++ )
  367.             {
  368.                 attrNode = attributes.item(index);
  369.                 name = attrNode.nodeName;
  370.                 value = attrNode.nodeValue;
  371.                 if ( name != 'id' )
  372.                     newNode.setAttribute(name, value);
  373.             }
  374.         }
  375.     
  376.         if ( node.nodeName == "SELECT" )
  377.         {
  378.             // copy options inside of SELECT
  379.             if ( newNode )
  380.             {
  381.                 for ( var index = 0; index < node.options.length; index++ )
  382.                 {
  383.                     var option = new Option(node.options.item(index).text,
  384.                                             node.options.item(index).value)
  385.                     newNode.add(option, null);
  386.                 }
  387.             }
  388.         }
  389.         else
  390.         {
  391.             // children of nodes
  392.             if ( node.childNodes )
  393.             {
  394.                 var childNode;
  395.                 
  396.                 for ( var child = 0; child < node.childNodes.length; child++ )
  397.                 {
  398.                     childNode = awCopyNodeAndChildren(node.childNodes[child]);
  399.                 
  400.                     newNode.appendChild(childNode);
  401.                 }
  402.             }
  403.         }
  404.  
  405.                 if ( node.nodeName == "INPUT" )
  406.                 {
  407.                         // copy the event handler, as it's not
  408.                         // sufficient to just set the attribute.
  409.                         newNode.onkeyup = node.onkeyup;
  410.                 }
  411.     }
  412.     
  413.     return newNode;
  414. }
  415.  
  416. function createHTML(tag)
  417. {
  418.     return document.createElementWithNameSpace(tag, "http://www.w3.org/TR/REC-html40");
  419. }
  420.  
  421. // remove row
  422.  
  423. function awRemoveRow(row)
  424. {
  425.     var body = document.getElementById('addressWidgetBody');
  426.     
  427.     awRemoveNodeAndChildren(body, awGetTreeItem(row));
  428.  
  429.     top.MAX_RECIPIENTS--;
  430. }
  431.  
  432. function awRemoveNodeAndChildren(parent, nodeToRemove)
  433. {
  434.     // children of nodes
  435.     var childNode;
  436.     
  437.     while ( nodeToRemove.childNodes && nodeToRemove.childNodes.length )
  438.     {
  439.         childNode = nodeToRemove.childNodes[0];
  440.     
  441.         awRemoveNodeAndChildren(nodeToRemove, childNode);
  442.     }
  443.     
  444.     parent.removeChild(nodeToRemove);
  445.  
  446. }
  447.  
  448. function awSetFocus(row, inputElement)
  449. {
  450.     top.awRow = row;
  451.     top.awInputElement = inputElement;
  452.     top.awFocusRetry = 0;
  453.     setTimeout("_awSetFocus();", 0);
  454. }
  455.  
  456. function _awSetFocus()
  457. {
  458.     var tree = document.getElementById('addressingWidgetTree');
  459.     try
  460.     {
  461.         theNewRow = awGetTreeRow(top.awRow);
  462.         //temporary patch for bug 26344
  463.         awFinishCopyNode(theNewRow);
  464.  
  465.         tree.ensureElementIsVisible(theNewRow);
  466.         top.awInputElement.focus();
  467.     }
  468.     catch(ex)
  469.     {
  470.         top.awFocusRetry ++;
  471.         if (top.awFocusRetry < 3)
  472.         {
  473.             dump("_awSetFocus failed, try it again...\n");
  474.             setTimeout("_awSetFocus();", 0);
  475.         }
  476.         else
  477.             dump("_awSetFocus failed, forget about it!\n");
  478.     }
  479. }
  480.  
  481.  
  482. //temporary patch for bug 26344
  483. function awFinishCopyNode(node)
  484. {
  485. //    dump ("awFinishCopyNode, node name=" + node.nodeName + "\n");
  486.     // Because event handler attributes set into a node before this node is inserted 
  487.     // into the DOM are not recognised (in fact not compiled), we need to parsed again
  488.     // the whole node and reset event handlers.
  489.  
  490.     var attributes = node.attributes;
  491.     if ( attributes && attributes.length )
  492.     {
  493.         var attrNode, name;
  494.         
  495.         for ( var index = 0; index < attributes.length; index++ )
  496.         {
  497.             attrNode = attributes.item(index);
  498.             name = attrNode.nodeName;
  499.             if (name.substring(0, 2) == 'on')
  500.             {
  501.                 node.setAttribute(name, attrNode.nodeValue);
  502. //                dump("  -->reset attribute " + name + "\n");
  503.             }
  504.         }
  505.     }
  506.     
  507.     // children of nodes
  508.     if ( node.childNodes )
  509.     {
  510.         var childNode;
  511.                 
  512.         for ( var child = 0; child < node.childNodes.length; child++ )
  513.             childNode = awFinishCopyNode(node.childNodes[child]);
  514.     }
  515. }
  516.  
  517.  
  518. function awFinishCopyNodes()
  519. {
  520.     for ( var row = 2; row <= top.MAX_RECIPIENTS; row++ )
  521.         awFinishCopyNode(awGetTreeRow(row));
  522. }
  523.  
  524.  
  525. function awTabFromRecipient(element, event)
  526. {
  527.     //If we are le last element in the tree, we don't want to create a new row.
  528.     if (element == awGetInputElement(top.MAX_RECIPIENTS))
  529.         top.doNotCreateANewRow = true;
  530. }